package demo.node;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * @author : feixiang.li
 * @since : 2025-09-19 14:16
 */
public class Main {


    static class Node {
        int index;
        int sum;
        List<Integer> list;

        public Node(int index, int sum, List<Integer> list) {
            this.sum = sum;
            this.list = list;
            this.index = index;
        }
    }

    public static void main(String[] args) {
//        int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] map = new int[10];
        int[] tmp = new int[10];
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] split = s.split(" ");

        int n = split[0].length();
        for (int i = 0; i < split[0].length(); i++) {
            map[split[0].charAt(i) - '0']++;
        }
        // 现在要维持一个长度为k的数组
        int k = Integer.parseInt(split[1]);
        int i = 0;
        // 初始化
        for (i = 1; i <= k; i++) {
            // 计算出当前位置的数组
            int a = i;
            while (a > 0) {
                tmp[a % 10]++;
                a /= 10;
            }
        }
        if (check(map, tmp)) {
            System.out.println(1);
        }
        for (int j = 2; j < 1001 - k; j++) {
            // 移除j-1的位置
            int a = j - 1;
            while (a > 0) {
                tmp[a % 10]--;
                a /= 10;
            }
            // 添加j+k-1的位置
            a = j + k - 1;
            while (a > 0) {
                tmp[a % 10]++;
                a /= 10;
            }
            if (check(map, tmp)) {
                System.out.println(j);
                return;
            }
        }
    }

    public static boolean check(int[] map, int[] tmp) {
        for (int i = 0; i < 10; i++) {
            if (map[i] != tmp[i]) {
                return false;
            }
        }
        return true;
    }
}
